/**
 * Copyright (C) 2010 Krishna Sundaresan <calvinkrishy@gmail.com>
 *      
 * This file is part of webos-datecalculator application.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

function IntervalDiffAssistant(){
}

IntervalDiffAssistant.prototype.setup = function(){

    this.startDateTime = new Date()
    this.endDateTime = new Date()
    
    this.dataModel = {
        value: false,
        "startdate": this.startDateTime,
        "starttime": this.startDateTime,
        "enddate": this.endDateTime,
        "endTime": this.endDateTime
    }
    
    /* setup widgets here */
    var model = new DateCalculatorModels(this.controller.sceneName)
        
    this.controller.setupWidget(Mojo.Menu.appMenu, DateCalculatorModels.menuAttrs, model.getAppMenuModel());
    this.controller.setupWidget(Mojo.Menu.viewMenu, undefined, model.getViewMenuModel());

    this.controller.setupWidget('igTime_chk', {}, this.dataModel)
    this.controller.setupWidget('edit_startDate', {
        modelProperty: 'startdate'
    }, this.dataModel)
    this.controller.setupWidget('edit_startTime', {
        modelProperty: 'starttime'
    }, this.dataModel)
    this.controller.setupWidget('edit_endDate', {
        modelProperty: 'enddate'
    }, this.dataModel)
    this.controller.setupWidget('edit_endTime', {
        modelProperty: 'endTime'
    }, this.dataModel)
    this.controller.setupWidget('disp_start_time', {
        unstyled: true
    }, {
        open: false
    })
    this.controller.setupWidget('disp_end_time', {
        unstyled: true
    }, {
        open: false
    })
    
    /* add event handlers to listen to events from widgets */
    this.handleCheckBind = this.handleCheck.bind(this)
    this.propertyChangeBind = this.dateChange.bind(this)
    this.controller.listen('igTime_chk', Mojo.Event.propertyChange, this.handleCheckBind)
    this.controller.listen('edit_startDate', Mojo.Event.propertyChange, this.propertyChangeBind)
    this.controller.listen('edit_startTime', Mojo.Event.propertyChange, this.propertyChangeBind)
    this.controller.listen('edit_endDate', Mojo.Event.propertyChange, this.propertyChangeBind)
    this.controller.listen('edit_endTime', Mojo.Event.propertyChange, this.propertyChangeBind)
    
};

IntervalDiffAssistant.prototype.handleCheck = function(event){

    if (event.value) {
        $('disp_start_time').mojo.setOpenState(true)
        $('disp_end_time').mojo.setOpenState(true)
        $('start_date_row').addClassName('first')
        $('end_date_row').addClassName('first')
        $('start_date_row').removeClassName('last')
        $('end_date_row').removeClassName('last')
    }
    else {
        $('disp_start_time').mojo.setOpenState(false)
        $('disp_end_time').mojo.setOpenState(false)
        $('start_date_row').addClassName('last')
        $('end_date_row').addClassName('last')
        $('start_date_row').removeClassName('first')
        $('end_date_row').removeClassName('first')
    }

    this.dateChange({})
}

IntervalDiffAssistant.prototype.dateChange = function(event){

    var start = this.dataModel.startdate
    var end = this.dataModel.enddate
    if (this.dataModel.startdate > this.dataModel.enddate) {
        start = this.dataModel.enddate
        end = this.dataModel.startdate
    }
    
    Mojo.Log.info('Between ' + start)
    Mojo.Log.info(' and ' + end)
	
    // If the date/time is the same do not display any message
    if (!this.dataModel.value) {
        if (start.getDate() == end.getDate() && start.getMonth() == end.getMonth() && start.getFullYear() == end.getFullYear()) {
            $('duration').update('')
            return
        }
    }
    else {
        if (start.getTime() == end.getTime()) {
            $('duration').update('')
            return
        }
    }
    
    var dur = getTimePeriodParts(start, end, this.dataModel.value)
    infoText = $L('Time interval is ') + periodFormatter(dur)
    $('duration').update(infoText)
    
}

IntervalDiffAssistant.prototype.cleanup = function(event){

    this.controller.stopListening('igTime_chk', Mojo.Event.propertyChange, this.handleCheckBind)
    this.controller.stopListening('edit_startDate', Mojo.Event.propertyChange, this.propertyChangeBind)
    this.controller.stopListening('edit_startTime', Mojo.Event.propertyChange, this.propertyChangeBind)
    this.controller.stopListening('edit_endDate', Mojo.Event.propertyChange, this.propertyChangeBind)
    this.controller.stopListening('edit_endTime', Mojo.Event.propertyChange, this.propertyChangeBind)
    
};
